package com.weixing.mall.provider.controller.rpc;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.api.model.dto.DeliveryAddrDto;
import com.weixing.mall.provider.api.model.vo.AgentVo;
import com.weixing.mall.provider.model.domain.AgentDeliveryAddress;
import com.weixing.mall.provider.service.IAgentDeliveryAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: kfc
 * @Description: <br/>
 * Date:Create in 2019/9/18 15:41
 * @Modified By:
 */
@RestController
@RequestMapping("/api/delivery")
public class AgentDeliveryRpcController extends BaseController {
    @Autowired
    private IAgentDeliveryAddressService agentDeliveryAddressService;


    @PostMapping("createDelivery")
    public Result<AgentVo> createDelivery(@RequestBody DeliveryAddrDto addrDto) {
        QueryWrapper<AgentDeliveryAddress> wrapper = new QueryWrapper<AgentDeliveryAddress>();
        wrapper.eq("agent_id",addrDto.getAgentId())
                .eq("contact_phone",addrDto.getContactPhone())
                .eq("contact_user",addrDto.getContactUser())
                .eq("province",addrDto.getProvince())
                .eq("city",addrDto.getCity())
                .eq("region",addrDto.getRegion())
                .eq("addr_detail",addrDto.getAddrDetail());
        List<AgentDeliveryAddress> list = agentDeliveryAddressService.list(wrapper);
        if (CollUtil.isEmpty(list)){
            AgentDeliveryAddress address = new AgentDeliveryAddress();
            BeanUtils.copyProperties(addrDto, address);
            agentDeliveryAddressService.save(address);
        }
//        if (null == addrDto.getAddrId()) {
//            AgentDeliveryAddress address = new AgentDeliveryAddress();
//            BeanUtils.copyProperties(addrDto, address);
//            agentDeliveryAddressService.save(address);
//        } else {
//            AgentDeliveryAddress address = agentDeliveryAddressService.getById(addrDto.getAddrId());
//            if (!address.getContactPhone().equals(addrDto.getContactPhone())
//                    || !address.getContactUser().equals(addrDto.getContactUser())
//                    || !address.getProvince().equals(addrDto.getProvince())
//                    || !address.getCity().equals(addrDto.getCity())
//                    || !address.getRegion().equals(addrDto.getRegion())
//                    || !address.getAddrDetail().equals(addrDto.getAddrDetail())) {
//                AgentDeliveryAddress address1 = new AgentDeliveryAddress();
//                BeanUtils.copyProperties(addrDto, address1);
//                agentDeliveryAddressService.save(address1);
//            }
//        }

        return ResultUtil.success();
    }

}
